package com.codename1.samples;


import com.codename1.components.ToastBar;
import com.codename1.io.File;
import com.codename1.io.FileSystemStorage;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import com.codename1.ui.TextArea;
import com.codename1.ui.TextField;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.FlowLayout;


/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ListFilesTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    private void appendChildren(StringBuilder sb, File f, int indent) {
        for (File child : f.listFiles()) {
            for (int i=0; i<indent; i++) {
                sb.append(' ');
            }
            sb.append(child.getName()).append("\n");
            if (child.isDirectory()) {
                appendChildren(sb, child, indent+2);
            }
        }
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", new BorderLayout());
        
        Button add = new Button("Add Directory");
        add.addActionListener(e->{
            callSerially(()->{
                Command ok = new Command("OK");
                Command cancel = new Command("Cancel");
                TextField fileName = new TextField("", "File Path", 20, TextField.NON_PREDICTIVE);
                if (ok == Dialog.show("File Path", fileName, new Command[]{ok, cancel})) {
                    File f = new File(fileName.getText());
                    if (!f.mkdir()) {
                        ToastBar.showErrorMessage("Failed to create directory");
                    }
                }
            });
        });
        
        Button rename = new Button("Rename File");
        rename.addActionListener(e->{
            callSerially(()->{
                Command ok = new Command("OK");
                Command cancel = new Command("Cancel");
                TextField fileName = new TextField("", "File Path", 20, TextField.NON_PREDICTIVE);
                TextField newName = new TextField("", "New Name", 20, TextField.NON_PREDICTIVE);
                if (ok == Dialog.show("File Path", BoxLayout.encloseY(fileName, newName), new Command[]{ok, cancel})) {
                    File f = new File(fileName.getText());
                    FileSystemStorage fs = FileSystemStorage.getInstance();
                    fs.rename(f.getPath(), newName.getText());
                }
            });
        });
        
        TextArea listing = new TextArea();
        Button refresh = new Button("Refresh");
        refresh.addActionListener(e->{
            File f = new File(FileSystemStorage.getInstance().getAppHomePath());
            StringBuilder sb = new StringBuilder();
            appendChildren(sb, f, 0);
            listing.setText(sb.toString());
                    
        });
        
        hi.add(BorderLayout.NORTH, FlowLayout.encloseCenter(add, rename)).add(BorderLayout.CENTER, listing).add(BorderLayout.SOUTH, refresh);
        
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
